﻿@model Tuple<List<deviceManage.Models.DeviceType>,int?>
@{
    ViewData["Title"] = "设备台账";
}
 
         

        <div class="layui-fluid">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto search" lay-filter="app-content-list">
                    <table>
                        <tr>
                            <td>
                                <div class="layui-inline">
                                    <label class="layui-form-label">设备名称</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">设备编号</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="deviceId" placeholder="请输入" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">外借时间</label>
                                    <div class="layui-input-inline" style="width:130px;">
                                        <input type="text" name="start" class="layui-input date" id="test-laydate-start" placeholder="开始日期">
                                    </div>
                                    
                                        -
                                    
                                    <div class="layui-input-inline" style="width:130px;">
                                        <input type="text" name="end" class="layui-input date" id="test-laydate-end" placeholder="结束日期">
                                    </div>
                                </div>
                            </td>
                            <td rowspan="2" style="padding-left:10px;">
                                <div class="layui-inline">
                                    <button style="height:75px;padding: 0 10px;"  class="layui-btn layuiadmin-btn-list" lay-submit="" lay-filter="search">
                                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查询
                                    </button>
                                </div>
                            </td>
                            <td rowspan="2" style="vertical-align:middle;padding-left:10px;">
                                @if (1 != Model.Item2)
                                {
                                    <button style="height:75px;padding: 0 8px;" onclick="goAddLog()" class="layui-btn">设备外借</button>
                                }

                            </td>
                        </tr>
                        <tr>
                            <td>
                                <div class="layui-inline">
                                    <label class="layui-form-label">设备类型</label>
                                    <div class="layui-input-inline">
                                        <select name="deviceTypeId">
                                            <option></option>
                                            @foreach (var type in Model.Item1)
                                            {
                                                <option value="@type.Id">@type.Name</option>
                                            }
                                        </select>
                                    </div>
                                </div>

                              
                                <div class="layui-inline">
                                    <label class="layui-form-label">归还时间</label>
                                    <div class="layui-input-inline" style="width:130px;">
                                        <input type="text" name="returnStart" class="layui-input date" id="returnStart" placeholder="开始日期" lay-key="122">
                                    </div>                                    
                                        -
                                    
                                    <div class="layui-input-inline" style="width:130px;">
                                        <input type="text" name="returnEnd" class="layui-input date" id="returnEnd" placeholder="结束日期" lay-key="123">
                                    </div>
                                </div>
                            </td>                            
                        </tr>
                    </table>
                      
                      

                     

                       
                    
                </div>

                <div class="layui-card-body">                    
                    <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
                </div>
            </div>
        </div>
    

<script type="text/javascript" src="~/js/deviceState.js"></script>
<script>
    

    var laydate = layui.laydate;
    var insStart = laydate.render({
        elem: '#test-laydate-start'
        , min: '1980-1-1'
        , done: function (value, date) {
            //更新结束日期的最小日期
            insEnd.config.min = lay.extend({}, date, {
                month: date.month - 1
            });

            //自动弹出结束日期的选择器
            insEnd.config.elem[0].focus();
        }
    });

    //结束日期
    var insEnd = laydate.render({
        elem: '#test-laydate-end'
        , min: '1980-1-1'
        , done: function (value, date) {
            //更新开始日期的最大日期
            insStart.config.max = lay.extend({}, date, {
                month: date.month - 1
            });
        }
    });

    var returnStart = laydate.render({
        elem: '#returnStart'
        , min: '2000-1-1'
        , done: function (value, date) {
            //更新结束日期的最小日期
            returnEnd.config.min = lay.extend({}, date, {
                month: date.month - 1
            });

            //自动弹出结束日期的选择器
            returnEnd.config.elem[0].focus();
        }
    });

    //结束日期
    var returnEnd = laydate.render({
        elem: '#returnEnd'
        , min: '2000-1-1'
        , done: function (value, date) {
            //更新开始日期的最大日期
            returnStart.config.max = lay.extend({}, date, {
                month: date.month - 1
            });
        }
    });

    form.render(null, 'app-content-list');

    //监听搜索
    form.on('submit(search)', function (data) {
        var field = data.field;
        //执行重载
        table.reload('LAY-app-content-list', {
            where: field
        });
    });
     

        //加载table
    var tableObj= table.render({
            elem: '#LAY-app-content-list'
            , url: '/deviceLend/LendLog/' //模拟接口
            ,method: 'post'
            , height:"full-150"
            , page: true
            , cols: [[
                
                { field: 'name', title: '设备名称', minWidth: 180, templet: '<div><a class="link" onclick="showDetail(\'{{d.device.id}}\')"  href="#" >{{d.device.name}}</a></div>'  }
                , { field: 'type', title: '设备类型', width: 100, templet: '<div>{{ d.device.deviceType.name}}</div>' }
                , { field: 'model', title: '规格型号', width: 200, templet: '<div>{{ d.device.model}}</div>' }
                , { field: 'deviceId', title: '设备编号', width: 120, templet: '<div>{{ d.device.deviceId}}</div>' }
                , { field: 'departName', title: '外借部门', width: 120, templet: '<div>{{ d.departName}}</div>' }
                , { field: 'lendUser', title: '外借人', width: 120, templet: '<div>{{ d.lendUser}}</div>' }
                , { field: 'lendDate', title: '外借时间', width: 120, templet: '<div>{{ getDate(d.lendDate)}}</div>' }  
                , { field: 'reason', title: '外借原因', width: 120, templet: '<div>{{ d.reason}}</div>' }
                , { field: 'lendDuration', title: '外借时长', width: 120, templet: '<div>{{ d.lendDuration}}</div>' }
                , { field: 'returnDate', title: '归还时间', width: 120, templet: '<div>{{ getDate(d.returnDate)}}</div>' } 
                , { field: 'returnUser', title: '归还人', width: 120, templet: '<div>{{ getstring(d.returnUser) }}</div>' } 
                , {
                    field: 'opr', title: '操作', width: 120, fixed:'right',
                templet:
                '<div class="layui-table-cell laytable-cell-11-0-8">'                
                    + ' {{ getButtons(d.id,d.state) }} '                    
                + '</div>'
            }
            ]]
            , skin: 'line'
        });

    function getDate(d) {
        if (d != null) {
            return d.substring(0, 10);
        } else {
            return "";
        }
    }
    function getstring(str) {
        if (str == null) return "";
        return str;
    }
    var viewFlag = "@Model.Item2";
    //根据状态获取不同的按钮
    function getButtons(id, state) {      
        if (viewFlag == "1") return "";
        if (state == 1) {
            return ' <a class="layui-btn layui-btn-warm layui-btn-xs" onclick="returnItem(\'' + id + '\')"><i class="layui-icon layui-icon-return"></i>设备归还</a> ';
        } else {
            return "";
        }
        
    }

    //删除记录
    function deleteItem(id) {
        layer.confirm("确定要删除记录吗？", function (index) {
            layer.close(index);
            $.post("/deviceInfo/DeleteRepairLog?id=" + id, function (res) {
                layer.msg(res.msg);
                if (res.code == 1) {
                    tableObj.reload();
                }
            });
        });
    }
    

    //添加页面
    var winIndex = null;
    function goAddLog() {
        $.get("/deviceLend/GoAddLendLog", function (str) {
            winIndex = layer.open({
                type: 1,
                area:["600px","500px"],
                title: '维修',
                content: str //注意，如果str是object，那么需要字符拼接。
            }); 
        });
    } 


    //归还页面
    function returnItem(id) {
        $.get("/deviceLend/GoReturnPage?id="+id, function (str) {
            winIndex = layer.open({
                type: 1,
                area: ["600px", "300px"],
                title: '设备归还',
                content: str //注意，如果str是object，那么需要字符拼接。
            });

           
        });
    } 

    function showDetail(id) {
        layer.open({
            mask: 0,
            type: 2,
            content: '/deviceInfo/Detail?id=' + id,
            area: ["1000px", "550px"]
        });
    }    
</script>

